using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Collections;

namespace DslEditorPowerToy.Installers
{
    /// <summary>
    /// This installer acts as a router to delegate to other installers based on passed in CustomActionData
    /// </summary>
    [RunInstaller(true)]
    public partial class MainInstaller : Installer
    {
        private const string InstallerType_Parameter = @"Installer";
        private const string StateProperty_Installer = @"Installers";

        #region Constructors
        public MainInstaller()
            : base()
        {
        }
        #endregion

        #region Public Methods
        public override void Install(IDictionary stateSaver)
        {
            DelegateInstaller();
            base.Install(stateSaver);
        }
        public override void Commit(System.Collections.IDictionary savedState)
        {
            DelegateInstaller();
            base.Commit(savedState);
        }
        public override void Rollback(System.Collections.IDictionary savedState)
        {
            DelegateInstaller();
            base.Rollback(savedState);
        }
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            DelegateInstaller();
            base.Uninstall(savedState);
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Determines which installer is addressed and adds it to internal collection
        /// </summary>
        private void DelegateInstaller()
        {
            //Determine which installer being used
            string installerType = this.Context.Parameters[InstallerType_Parameter];
            if (false == string.IsNullOrEmpty(installerType))
            {
                ChildInstaller installer = ChildInstaller.None;
                installer = (ChildInstaller)Enum.Parse(typeof(ChildInstaller), installerType, true);
                if (ChildInstaller.None != installer)
                    AddInstaller(installer);
            }
        }
        /// <summary>
        /// /Adds an installer to installer collection
        /// </summary>
        /// <param name="installer"></param>
        private void AddInstaller(ChildInstaller installer)
        {
            Installer installerClass = null;
            switch (installer)
            {
                case ChildInstaller.LaunchFile:
                    installerClass = new LaunchFileInstaller();
                    break;

                case ChildInstaller.ResetVS:
                    installerClass = new ResetVsInstaller();
                    break;
            }

            //Add installer to internal collection
            if (null != installerClass)
                this.Installers.Add(installerClass);
        }

        #endregion
    }
}
